package com.yang.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.mall.dto.LoginDTO;
import com.yang.mall.entity.Member;
import com.yang.mall.entity.SysUser;
import com.yang.mall.exception.OLException;
import com.yang.mall.mapper.SysUserMapper;
import com.yang.mall.service.ISysUserService;
import com.yang.mall.tools.BeanUtils;
import com.yang.mall.tools.JWTUtils;
import com.yang.mall.vo.MemberVO;
import com.yang.mall.vo.SysUserVO;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author kadorlin
 * @since 2022-10-21
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    /**
     * 登陆
     * @param loginDTO
     * @author:kadorlin
     * @date:2022/10/24 15:43
     * @return:com.yang.mall.vo.SysUserVO
     **/
    @Override
    public SysUserVO login(LoginDTO loginDTO) {
        QueryWrapper<SysUser> qw  = new QueryWrapper<SysUser>();
        qw.lambda()
                .eq(SysUser::getPassword,loginDTO.getPassword())
                .eq(SysUser::getUsername,loginDTO.getUsername());
        SysUser sysUser = this.getOne(qw);
        if(Objects.isNull(sysUser)){
            throw new OLException("用户名或密码错误~");
        }
        if(!sysUser.getStatus().equals("1")){
            throw new OLException("用户审核中，请稍后再试~");
        }
        SysUserVO sysUserVO = BeanUtils.copyBeanProp(sysUser, SysUserVO.class);
        //432000000 一个月
        String token = JWTUtils.createJWT(sysUserVO.getUserId().toString(), 432000000);//5天过期
        sysUserVO.setToken(token);
        sysUserVO.setPassword(null);
        return sysUserVO;
    }


}
